/* eslint-disable max-lines */
import * as BASE from '@/stores/definitions-base';
import { t } from '@lingui/macro';
import React from 'react';
import { Input, Rating, Switch } from '@alifd/next';

const {
  DATETIME_TEXT,
  DATE_TEXT,
  TIME_TEXT,
  EMPTY_TEXT,
  ENUMS: ENUMSBase,
  FieldsGrid: FieldGridBase,
  TitleGrid,
  column: columnBase,
  toSelectDataSource,
} = BASE;
export { DATETIME_TEXT, DATE_TEXT, EMPTY_TEXT, TIME_TEXT, TitleGrid, toSelectDataSource };

/**
 * @param {*} path        属性path
 * @param {*} props       column属性
 * @returns column定义
 */
export function column(...args) {
  return columnBase(model, ...args);
}

/**
 * 显示一组表单字段
 * ```js
 * {
 *   fields: string[],    // 字段名称，可以使用.来指定嵌套对象属性，例如order.busOrder.shift
 *   columns: number,     // 列数，默认为4,最大为12,最小为1
 *   model: object,       // 字段定义数据模型的根对象，默认不指定，仅在使用子对象作为根模型时设置
 *   overrideModel: object, // 重载字段定义数据模型
 *   mode: string,        // 表单模式，默认为edit，可选view
 *   itemLayout: object,  // FormItem布局配置，默认为{}
 * }
 * ```
 *
 * @param {object} param0
 * @returns
 */
export function FieldsGrid(props) {
  return <FieldGridBase model={model} {...props} />;
}

//----------------------------------------------------------------------
export const ENUMS = {
  ...ENUMSBase,

  enable: {
    true: t`启用`,
    false: t`禁用`,
  },

  priceType: {
    0: t`普通类型`,
    1: t`澳门线路`,
  },
};

/**
 * 模型定义:
 * 1. 普通文本仅需要定义label
 * 2. 需要通过枚举转换的值，定义text; 当使用renderer返回组件时，text属性无效，需要通过组件的renderPreview来指定预览时的绘制方式
 * 3. 通用的ui属性需要定制时，定义ui; 特定模式(view, edit, add)下的ui，添加后缀定义为ui_<mode>，例如：ui_view, ui_edit, ui_add, ui_table
 * 4. ui可配置属性包括:
 *    renderer: 内容渲染函数, Form表单中的原型为：(value: any, formData: any, fieldDefinition: any) => ReactNode; Table Column中的原型为: (value: any, columnIndex: number, rowData: any) => ReactNode;
 *    itemProps: 表单Form.Item的属性，可以是对象，或函数({ isPreview: boolean, mode: string, value: any, formData: any, ...otherDefinitions}) => void;
 *    span: Grid Cell占用的列数, 默认为1
 *
 * 【注】获取接口的model定义后，vscode中通过正则替换：^(\s+)"label": "([^']+)"  ===> $1label: t`$2`
 */
export const model = {
  groupId: {
    label: t`群id`,
  },
  groupName: {
    label: t`群名`,
    ui: {
      itemProps: { required: true },
    },
  },
  enable: {
    label: t`状态`,
    text: (v, r) => <Switch checked={v} size="small" unCheckedChildren="禁用" checkedChildren="启用" autoWidth onChange={() => rootStore.driverGroupStore.updateState(r)} />,
    ui: {
      renderer: (v) => <Switch checked={v} size="small" unCheckedChildren="禁用" checkedChildren="启用" autoWidth />,
    },
  },
  managerId: {
    label: t`管理员id`,
  },
  managerName: {
    label: t`管理员名字`,
    ui: {
      itemProps: { required: true },
    },
  },
  managerPhone: {
    label: t`管理员电话`,
    ui: {
      itemProps: { required: true, format: 'tel' },
    },
  },
  accountingId: {
    label: t`会计id`,
  },
  accountingName: {
    label: t`会计名字`,
    ui: {
      itemProps: { required: true },
    },
  },
  accountingPhone: {
    label: t`会计电话`,
    ui: {
      itemProps: { required: true, format: 'tel' },
    },
  },
  description: {
    label: t`描述`,
    ui: {
      renderer: (v) => <Input.TextArea value={v} />,
      span: 3,
    },
  },
  minimumPoints: {
    label: t`最低接单积分`,
  },
  priceRules: {
    label: t`计分规则`,
    priceType: {
      label: t`线路类型`,
      text: (v) => <>{ENUMS.priceType[v]}</>,
    },
    startPrice: {
      label: t`开始价格`,
    },
    endPrice: {
      label: t`结束价格`,
    },
    issueOrderPoint: {
      label: t`发单积分`,
    },
    takeOrderPoint: {
      label: t`接单积分`,
    },
    remarks: {
      label: t`备注`,
    },
  },
  crBy: {
    label: t`创建者`,
  },
  upBy: {
    label: t`最后修改者`,
  },
  crDate: {
    label: t`创建时间`,
    text: DATETIME_TEXT,
  },
  upDate: {
    label: t`最后修收时间`,
  },
  tenantId: {
    label: t`租户ID`,
  },
  id: {
    label: '',
  },
  // 出发时间 - 紧急订单标准 > 当前时间 则为 紧急订单,反之亦然
  urgentOrderRule: {
    label: t`紧急订单标准(分)`,
  },
  reserveOrderInterval: {
    label: t`预约单通知间隔(秒)`,
  },
  urgentOrderInterval: {
    label: t`急单通知间隔(秒)`,
  },
  takeOrderInterval: {
    label: t`用户抢单时间间隔(分)`,
  },
  // 前端新增
  managerGroup: {
    label: t`管理员/电话`,
    text: (v, r) => {
      console.log(r,11111);
      const {
        managerName = '',
        managerPhone = '',
      } = r;
      return <>{`${managerName} / ${managerPhone}`}</>;
    },
  },
  accountingGroup: {
    label: t`会计/电话`,
    text: (v, r) => {
      const {
        accountingName = '',
        accountingPhone = '',
      } = r;
      return <>{`${accountingName} / ${accountingPhone}`}</>;
    },
  },
};
